package com.wxx.weixin.controller;

import com.google.common.collect.Maps;
import com.wxx.weixin.common.Const;
import com.wxx.weixin.util.AppUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;

@Controller
@RequestMapping("/check/")
public class CheckToken {

    @RequestMapping(value = "checkToken",method = RequestMethod.GET)
    public void checkTokenGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        String[] arr = new String[]{timestamp,nonce, Const.TOKEN};
        Arrays.sort(arr);

        StringBuffer sb = new StringBuffer();
        for (String s : arr) {
            sb.append(s);
        }
        String sha1 = AppUtil.getSha1(sb.toString());
        PrintWriter out = response.getWriter();
        if (sha1.equals(signature)){
            out.write(echostr);
        }else{
            out.write("");
        }

    }


    @RequestMapping(value = "checkToken",method = RequestMethod.POST)
    public void checkTokenPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        Map<String, String> map = AppUtil.getMap(request);
        String msgType = map.get("MsgType");
        //开发者微信号
        String toUserName = map.get("ToUserName");
        //发送方帐号
        String fromUserName = map.get("FromUserName");
        //消息id，64位整型
        String msgId = map.get("MsgId");

        StringBuffer sb = new StringBuffer();
        sb.append("欢迎关注，请选择您想了解的英雄，\n输入\"?\"返回菜单\n");
        sb.append("1.狐狸\n");
        sb.append("2.拉克丝\n");
        sb.append("3.返回菜单\n");

        //回复文本的处理
        if (msgType.equals(Const.MSGTYPE.TEXT)){
            //类型为text，获取里面的值进行回复

            //获取文本内容
            String content = map.get("Content");
            if ("1".equals(content)){
                String desc = "九尾妖狐是一个高技能伤害的英雄，她可以不断在团战边缘游走并且凭借自己短CD的技能持续输出伤害。并且漂亮的法师型刺客，声音甜美销魂，身材苗条性感，是造型控的首选之一。";
                String str = sendMessage(fromUserName, toUserName, desc);
                out.write(str);
            }else if ("2".equals(content)){
                String desc = "拉克丝是一个远程输出法师，所有技能均为非锁定目标的，所以释放技能时需要玩家具有一定的预判能力。技能释放的距离较远，若法术强度足够，可以在对手打不到自己的情况下一套技能可以杀死脆皮英雄。但伤害技能初始伤害低，护盾技能经历几个版本的改动，现在所能承受的伤害非常低，初始血量低，自身也是一个非常脆的英雄，所以应尽量避免与对方贴身战斗。";
                String str = sendMessage(fromUserName,toUserName,desc);
                out.write(str);
            }else if ("？".equals(content)){
                String str = sendMessage(fromUserName, toUserName, sb.toString());
                out.write(str);
            }else if ("?".equals(content)){
                String str = sendMessage(fromUserName, toUserName, sb.toString());
                out.write(str);
            }
        }else if (msgType.equals(Const.MSGTYPE.EVENT)){
            //第一次关注发送的消息
            //消息id，64位整型
            String event = map.get("Event");
            if (event.equals(Const.MSGTYPE_EVENT.SUBSCRIBE)){
                //关注时，发送一条欢迎消息
                String str = sendMessage(fromUserName, toUserName, sb.toString());
                out.write(str);
            }
        }

        System.err.println(map.toString());
    }


    //发送消息 map-xml
    private String sendMessage(String toUserName,String fromUserName,String content) throws Exception {
        Map<String,String> map = Maps.newHashMap();
        map.put("ToUserName",toUserName);
        map.put("FromUserName",fromUserName);
        map.put("CreateTime","sddf");
        map.put("MsgType","text");
        map.put("Content",content);
        return AppUtil.mapToXml(map);
    }
}
